<?php

/**
 * TopAdmin
 * 版权所有 2018-2027 zyncms，并保留所有权利。
 * Author: 御宅男 <530765310@qq.com>
 * Date: 2021/2/24
 */
namespace app\admin\controller\config;

use app\admin\model\AuthDept;
use app\admin\model\Config as ConfigModel;
use app\common\controller\Backend;
use app\common\model\ConfigGroup as ConfigGroupModel;
use app\common\traits\Curd;
use think\facade\Db;
use think\facade\Request;
use think\facade\View;

class Group extends Backend
{
    use Curd;
    public $banfie;
    protected $modelClass = null;

    public function initialize()
    {
        parent::initialize();
        //允许使用的字段列表
        $this->banfie = array("text", "checkbox", "textarea", "radio", "number", "datetime", "image", "images", "array", "switch", "select", "Ueditor", "file", "files", 'color', 'tags');
        $this->modelClass = new ConfigGroupModel;
    }




//    配置分组
    public function add(){
        if(Request::isPost()){
            $data = $this->request->post();
            $info = ConfigGroupModel::where('name',$data['name'])->find();
            if($info){
                $this->error(lang('field already exist'));

            }else {
                $model = new ConfigGroupModel();
                if ($model->add($data)) {
                    $this->success(lang('edit success'));
                } else {
                    $this->error(lang('edit fail'));
                }
            }

        }
        $view = ['title'=>lang('config group'),'info'=>''];
        View::assign($view);
        return view('add');

    }

    // 配置分组修改
    public function edit()
    {
        if (Request::isPost()) {
            $data = $this->request->post();
            if($data['id']==1){
                $this->error(lang('supper man cannot edit'));
            }
            try{
                $this->validate($data, 'ConfigGroup');
            }catch (\Exception $e){
                $this->error($e->getMessage());
            }
            $where['id'] = $data['id'];
            $res = ConfigGroupModel::update($data, $where);
            if($res){

                $this->success(lang('edit success'), url('system.Config/group'));
            }else{
                $this->error(lang('edit fail'));

            }

        } else {
            $id = Request::param('id');
            $info = ConfigGroupModel::find(['id' => $id]);
            $view = [
                'info' => $info,
                'title' => lang('edit')
            ];
            View::assign($view);
            return view('add');
        }
    }


}
